package com.yhcrt.service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Resource;
import com.yhcrt.utils.DateUtil;
import com.yhcrt.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.yhcrt.dao.DaoSupport;
import com.yhcrt.entiy.Group;
import com.yhcrt.utils.Constants;

/**
 * 系统参数的service
 * 2021年10月14日
 * 
 */
@Service("CommonService")
public class CommonService {
	@Resource
	private DaoSupport dao;

	//加载项目数据常量 add by Rocye in 2024.06.11
	public List<Map<String, String>> loadProjectList(Integer matchId){
		try {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("matchId", matchId);
			List<Map<String, String>> datalist = (List<Map<String, String>>) dao.findForList("CommonMapper.loadProjectList", param);
			return datalist;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ArrayList<Map<String, String>>();
	}
	
	/**
	 * 
	 * @Description: TODO
	 * @return String
	 */
	public String getSplicingdo(){
	
		return null;
	}
	/**
	 * 
	 * @Description: 查詢指定项目和父节点的组别集合
	 * @return String
	 */
	public String findGroupStr(String parentIds,Integer pid){
		String str  = "";
		int index = parentIds.indexOf("/"+pid+"/");
		String [] strs = parentIds.substring(index).split("/");
		List<Integer> list = com.yhcrt.utils.StringUtils.array2List(strs);
		list.remove(pid);
		if(list.size()==0){
			return str;
		}
		try {
			Map<String, Object> map = new HashMap<>();
			map.put("list", list);
			map.put("matchId", Constants.matchId);
			str = (String) dao.findForObject("GroupMapper.findGroupNameStr", map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(!StringUtils.isBlank(str)){
			str = str.replace(",", "|");
		}else{
			str = "";
		}
		return str;
	}
    
	/**
	 * 获取决赛日期列表
	 * @param pid
	 * @return
	 */
//	public List<String> getDateList(Integer pid){
//		try {
//			List<String> datelist = (List<String>) dao.findForList("CommonMapper.getDateList", pid);
//			return datelist;
//		} catch (Exception e) {
//			e.printStackTrace();
//		}   
//		return new ArrayList<String>();
//	}
	public List<String> getDateList(Integer matchId,Integer typeId){
		try {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("matchId", matchId);
			param.put("typeId", typeId);
			List<String> datelist = (List<String>) dao.findForList("CommonMapper.getDateList", param);
			return datelist;
		} catch (Exception e) {
			e.printStackTrace();
		}   
		return new ArrayList<String>();
	}
	
//	@SuppressWarnings("all")
//	public List<List<Map<String, Object>>> getIndexForJJTY(List<String> dateList) {
//		List<Map<String, Object>> listdata =null ;
//		List<List<Map<String, Object>>> result = new ArrayList<List<Map<String,Object>>>();
//		try {     
//			//获取所有的项目
//			List<Map<String, Object>> projectNameList = (List<Map<String, Object>>)dao.findForList("CommonMapper.getProjectNameForJJTY");
//			//获取比赛数据
//		    listdata = (List<Map<String, Object>>)dao.findForList("CommonMapper.getIndexForJJTY");
//		    Long totalNumSum=0l; //合计的项目总数
//		    for(Map<String, Object> projectName : projectNameList ){
//		    	List<Map<String, Object>> listtemp = new ArrayList<Map<String,Object>>();
//		    	Map<String, Object> mapFirst = new HashMap<String, Object>();
//		    	mapFirst.put("name", projectName.get("className"));
//		    	mapFirst.put("classId", projectName.get("classId"));
//		    	listtemp.add(mapFirst);
//		    	Long totalNum = 0l;
//		    	for(String date : dateList){
//		    		Map<String, Object> map = new HashMap<String, Object>();
//		    		for(Map<String, Object> o : listdata){
//		    			if((o.get("classId").toString()).equals((projectName.get("classId").toString()))){
//			    			if(date.equals(o.get("createDate"))){
//			    				map.put("createDate", o.get("createDate"));
//					    		map.put("name", o.get("totalNum"));
//					    		totalNum += (Long)o.get("totalNum");
//					    		map.put("classId", o.get("classId"));
//					    		listdata.remove(o);
//					    		break;
//			    			}
//		    			}
//		    		}
//	    			if(date.equals(projectName.get("creaTime"))){
//	    				map.put("createDate", projectName.get("creaTime"));
//	    				map.put("finalFlag", false);
//	    				if(totalNum.longValue()==(Long)projectName.get("totalNum")){	
//	    					if(!map.containsKey("name")){
//	    						map.remove("createDate");
//	    					}
//	    					map.put("finalFlag", true);
//	    				}
//	    				map.put("total", (Long)projectName.get("totalNum")-totalNum);
//	    			}
//		    		listtemp.add(map);
//		    	}
//		    	Map<String, Object> mapLast = new HashMap<String, Object>();
//		    	mapLast.put("name", totalNum);
//		    	totalNumSum += (Long)projectName.get("totalNum");
//		    	mapLast.put("totalNum", projectName.get("totalNum"));
//		    	listtemp.add(1,mapLast);
//		    	result.add(listtemp);
//		    }
//		    List<Map<String, Object>> totalList = new ArrayList<Map<String, Object>>();
//		    Map<String, Object> mapFirst = new HashMap<String, Object>();
//	    	mapFirst.put("name", "合计");
//	    	totalList.add(mapFirst);
//	    	long[] tempList = new long[dateList.size()+1];
//		    for(List<Map<String, Object>> list2 : result){
//		    	for(int i=1;i<list2.size();i++){
//		    		if(list2.get(i)!=null && list2.get(i).get("name") != null){
//		    			tempList[i-1] += (Long)list2.get(i).get("name");
//		    		}
//		    	}
//		    }
//		    boolean flag = true;
//		    for(Long num : tempList){
//		    	Map<String, Object> map = new HashMap<String, Object>();
//		    	map.put("name", num);
//                if(flag){
//                	map.put("totalNum", totalNumSum);
//                	flag = false;
//                }
//               	totalList.add(map);
//		    }
//		    result.add(totalList);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return result;
//	}
	@SuppressWarnings("all")
	public List<Object> getIndexForWeb(Integer matchId,Integer typeId) {
		List<Map<String, Object>> listdata =null ;
		List<Object> result = new ArrayList<Object>();
		try {     
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("matchId", matchId);
			param.put("typeId", typeId);
		    listdata = (List<Map<String, Object>>)dao.findForList("CommonMapper.getIndexForWeb",param);
		    List<Map<String, Object>>  pro_totalNumList = new ArrayList<Map<String,Object>>();  //存储项目--报名总数
		    List<Map<String, Object>>  pro_date_numList = new ArrayList<Map<String,Object>>();  //存储日期--数量
		    Integer checkClassId = null;
		    String checkDate = null;
		    Map<String, Object> mapName = null;  
		    Map<String, Object> mapDate = null;  
		    for(Map<String, Object> dbMap : listdata) {     //遍历数据库，组装list
		    	Integer classId = (Integer) dbMap.get("classId");
		    	Long  dateNum = (Long) dbMap.get("dateNum");
		    	String className = (String)dbMap.get("className");
		    	String groupName = (String)dbMap.get("groupName");
		    	String dataDate = DateUtil.DateToStr((Date)dbMap.get("dataDate"));
		    	if(!classId.equals(checkClassId)) {  
		    		if(mapName!=null) {
		    			pro_totalNumList.add(mapName);
		    		}
		    		mapName = new HashMap<String, Object>();		    		
		    		mapName.put("name",className);
		    		mapName.put("groupName",groupName);
		    		mapName.put("classId",classId);
		    		mapName.put("value",dateNum);
		    		checkClassId = classId;		
		    		
		    		if(mapDate!=null) {
		    			pro_date_numList.add(mapDate);
		    		}	
		    		mapDate = new HashMap<String, Object>();
		    		mapDate.put(dataDate, dateNum);
		    		mapDate.put("name",className);
		    		mapDate.put("classId", classId.longValue());
		    	}else {
		    		mapName.put("name",className);
		    		mapName.put("classId",classId);
		    		mapName.put("groupName",groupName);
		    		mapName.put("value",Long.valueOf(mapName.get("value").toString())+dateNum);
		    		
		    		mapDate.put(dataDate, dateNum);
		    		mapDate.put("name",className);
		    		mapDate.put("classId", classId.longValue());
		    	}
		    }		
		    if(mapName!=null) {
    			pro_totalNumList.add(mapName);
    		}
		    if(mapDate!=null) {
		    	pro_date_numList.add(mapDate);
    		}
		    result.add(pro_totalNumList);
		    result.add(pro_date_numList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	
	
/*	@SuppressWarnings("all")
	public List<List<Map<String, Object>>> getIndexForQMJS(List<String> dateList) {
		List<Map<String, Object>> listdata =null ;
		List<List<Map<String, Object>>> result = new ArrayList<List<Map<String,Object>>>();
		try {     
			//获取所有的项目
			List<Map<String, Object>> projectNameList = (List<Map<String, Object>>)dao.findForList("CommonMapper.getProjectNameForQMJS");
			//获取比赛数据
		    listdata = (List<Map<String, Object>>)dao.findForList("CommonMapper.getIndexForQMJS");
		    String parentName = ""; //为页面的table第一列合并单元格的处理
		    Long totalNumSum = 0l;
		    for(Map<String, Object> projectName : projectNameList ){
		    	List<Map<String, Object>> listtemp = new ArrayList<Map<String,Object>>();
		    	Map<String, Object> mapFirst = new HashMap<String, Object>();	
		    	mapFirst.put("parentId", projectName.get("parentId"));
		    	mapFirst.put("parentName", projectName.get("parentName"));
		    	mapFirst.put("className", projectName.get("className"));
		    	if(!parentName.equals(projectName.get("parentName"))){
		    		mapFirst.put("num", projectName.get("num"));
		    		parentName = (String) projectName.get("parentName");
		    	}
		    	mapFirst.put("classId", projectName.get("classId"));
		    	listtemp.add(mapFirst);
		    	Long totalNum = 0l;
		    	for(String date : dateList){
		    		Map<String, Object> map = new HashMap<String, Object>();
		    		for(Map<String, Object> o : listdata){
		    			if((o.get("classId").toString()).equals((projectName.get("classId").toString()))){
			    			if(date.equals(o.get("createDate"))){
			    				map.put("createDate", o.get("createDate"));
					    		map.put("name", o.get("totalNum"));
					    		totalNum += (Long)o.get("totalNum");
					    		map.put("classId", o.get("classId"));
					    		listdata.remove(o);
					    		break;
			    			}
		    			}
		    		}
	    			if(date.equals(projectName.get("creaTime"))){
	    				map.put("createDate", projectName.get("creaTime"));
	    				map.put("finalFlag", false);
	    				if(totalNum.longValue()==(Long)projectName.get("totalNum")){
	    					if(!map.containsKey("name")){
	    						map.remove("createDate");
	    					}
	    					map.put("finalFlag", true);
	    				}
	    				map.put("total", (Long)projectName.get("totalNum")-totalNum);
	    			}
		    		listtemp.add(map);
		    	}
		    	Map<String, Object> mapLast = new HashMap<String, Object>();
		    	mapLast.put("name", totalNum);
		    	totalNumSum += (Long)projectName.get("totalNum");
		    	mapLast.put("totalNum", projectName.get("totalNum"));
		    	listtemp.add(1,mapLast);
		    	result.add(listtemp);
		    }
		    List<Map<String, Object>> totalList = new ArrayList<Map<String, Object>>();
		    Map<String, Object> mapFirst = new HashMap<String, Object>();
	    	mapFirst.put("num", 1);
	    	mapFirst.put("className", "合计");
	    	totalList.add(mapFirst);
	    	long[] tempList = new long[dateList.size()+1];
		    for(List<Map<String, Object>> list2 : result){
		    	for(int i=1;i<list2.size();i++){
		    		if(list2.get(i)!=null && list2.get(i).get("name") != null){
		    			tempList[i-1] += (Long)list2.get(i).get("name"); 
		    		}
		    	}
		    }
		    boolean flag = true;
		    for(Long num : tempList){
		    	Map<String, Object> map = new HashMap<String, Object>();
		    	map.put("name", num);
                if(flag){
                	map.put("totalNum", totalNumSum);
                	flag = false;
                }
               	totalList.add(map);
		    }
		    result.add(totalList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}    */


	
	
	@SuppressWarnings("all")
	public List<Map<String, Object>> getRecordFor(String type,String order) throws Exception {
		Map<String, Object> map = new HashMap<>();
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		
		map.put("typeId", type);
		map.put("order", order);
		map.put("matchId", Constants.matchId);
		try {
			List<Map<String, Object>> listdata  =(List<Map<String, Object>>)dao.findForList("ComRecordMapper.query",map);
			List<Map<String, Object>> list= new ArrayList<Map<String,Object>>();
			Map<String, Object> resMap = new HashMap<String, Object>();
			if (listdata.size() > 0) {
				if (order.equals("1")) {
					//时间分组
					String name = listdata.get(0).get("createTime").toString();
					for(Map<String, Object> map2 : listdata){
						if(!name.equals(map2.get("createTime"))){
							resMap.put("key", name);
							resMap.put("value", list);
							result.add(resMap);
							list = new ArrayList<Map<String, Object>>();
							resMap = new HashMap<String, Object>();
							name = map2.get("createTime").toString();
						}
						list.add(map2);
					}
					resMap.put("key", name);
					resMap.put("value", list);
					result.add(resMap);
				}else {
					//项目分组
					String name = listdata.get(0).get("parentName").toString();
					for(Map<String, Object> map2 : listdata){
						if(!name.equals(map2.get("parentName"))){
							resMap.put("key", name);
							resMap.put("value", list);
							result.add(resMap);
							list = new ArrayList<Map<String, Object>>();
							resMap = new HashMap<String, Object>();
							name = map2.get("parentName").toString();
						}
						list.add(map2);
					}
					resMap.put("key", name);
					resMap.put("value", list);
					result.add(resMap);
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@SuppressWarnings("all")
	public List<List<Map<String, String>>> getRecordForQMJS(String type) {
		List<Map<String, String>> listdata = null;
		List<List<Map<String, String>>> result = new ArrayList<List<Map<String,String>>>();
		try {
			listdata =(List<Map<String, String>>)dao.findForList("CommonMapper.getRecordForQMJS",type);
			List<Map<String, String>> list= new ArrayList<Map<String,String>>();
			if(listdata.size()>0){
				if("date".equals(type)){
					String name = listdata.get(0).get("recordTime");
					for(Map<String, String> map : listdata){
						if(!name.equals(map.get("recordTime"))){
							result.add(list);
							list = new ArrayList<Map<String, String>>();
							name = map.get("recordTime");
						}
						list.add(map);
					}
					result.add(list);
				}else{
					String name = listdata.get(0).get("parentName");
					for(Map<String, String> map : listdata){
						if(!name.equals(map.get("parentName"))){
							result.add(list);
							list = new ArrayList<Map<String, String>>();
							name = map.get("parentName");
						}
						list.add(map);
					}
					result.add(list);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return result;
	}
	
	@SuppressWarnings("all")
	public List<Map<String, Object>> queryReportInfo(Integer pid) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map.put("pid", pid);
		map.put("matchId", Constants.matchId);
		List<Map<String, Object>> list = (List<Map<String, Object>>) dao.findForList("CommonMapper.queryReportInfo", map);
		return list;
	}
	
	public List<Map<String, Object>> queryReportDetail(Integer uid,Integer type,Integer pid) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("unitId", uid.toString());
		map.put("type", type);
		map.put("pid", pid);
		map.put("matchId", Constants.matchId);
		List<Map<String, Object>> list = (List<Map<String, Object>>) dao.findForList("CommonMapper.queryReportDetail", map);
		//通过数据库将运动员ID转运动员名称太费时间了，改成JAVA处理	modify by Rocye in 2024.06.08
		String athleteIds = list.stream().map(b->String.valueOf(b.get("athleteIds"))).collect(Collectors.joining(","));
		String newAthleteIds = Stream.of(StringUtils.split(athleteIds, ',')).distinct().collect(Collectors.joining(","));
		Map<String,Object> pmap = new HashMap<>();
		pmap.put("ids", newAthleteIds);
		List<Map<String, Object>> athleteList = (List<Map<String, Object>>) dao.findForList("AthleteInfoMapper.findAthleteNameList", pmap);
		Map<String, String> athleteId2NameMap = athleteList.stream().collect(Collectors.toMap(b->String.valueOf(b.get("id")), b->String.valueOf(b.get("name")), (oldVal, newVal)->oldVal));
		for (Map<String, Object> item : list) {
			String athleteNames = Stream.of(StringUtils.split(String.valueOf(item.get("athleteIds")), ',')).map(b->athleteId2NameMap.get(b)).collect(Collectors.joining(","));
			item.put("athleteName", athleteNames);
		}
		return list;
	}

	//兼容达梦8修改 update by Rocye 2024.05.30
	public List<Map<String, Object>> getRankingByTClass(String tClass) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tClass", tClass);
		map.put("matchId", Constants.matchId);
		try {
			List<Map<String, Object>> list = (List<Map<String, Object>>) dao.findForList("CommonMapper.getRankingByTClass",map);
			int size = list.size();
			for (int i = 0; i < size; i++) {
				Map<String, Object> item = list.get(i);
				item.put("rank", i+1);
				if (i > 0) {
					Map<String, Object> lastItem = list.get(i-1);
					if (String.valueOf(item.get("score")).equals(String.valueOf(lastItem.get("score")))) {
						item.put("rank", lastItem.get("rank"));
					}
				}
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<Map<String, Object>> getMedalDetailForzdxs(String unit, Integer type) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("unit", unit);
		map.put("type", type);
		map.put("matchId", Constants.matchId);
		try {
			return (List<Map<String, Object>>) dao.findForList("CommonMapper.getMedalDetailForzdxs",map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public Integer getSpecialJNum() {
		try {
			Map<String, Object> map = new HashMap<>();
			map.put("matchId", Constants.matchId);
			return (Integer) dao.findForObject("CommonMapper.getSpecialJNum", map);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
	public Integer getSpecial2JNum(){
		try {
			Map<String, Object> map = new HashMap<>();
			map.put("matchId", Constants.matchId);
			return (Integer) dao.findForObject("CommonMapper.getSpecial2JNum", map);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
}
